package doubanhongniang

import grails.test.*

class DoubanUserTests extends GrailsUnitTestCase {
	protected void setUp() {
		super.setUp()
	}
	
	protected void tearDown() {
		super.tearDown()
	}
	
	void testConstraints() {
		def will = new DoubanUser(uid: "william")
		mockForConstraintsTests(DoubanUser, [ will ])
		
		def testUser = new DoubanUser()
		assertFalse testUser.validate()
		assertEquals "nullable", testUser.errors["uid"]
		assert !testUser.errors['location']
		//assert !testUser.errors['email']
		
		testUser = new DoubanUser(uid: "william", location: "shanghai", email:'abc@gmail.com')
		assertFalse testUser.validate()
		assertEquals "unique", testUser.errors["uid"]
		assert !testUser.errors['location']
		assert !testUser.errors['email']
		
		testUser = new DoubanUser(uid: "mary", email: "abc.com")
		assertFalse testUser.validate()
		assertEquals "email", testUser.errors['email']
		assert !testUser.errors['uid']
		testUser = new DoubanUser(uid: "mary", location:'shanghai', email: "abc@gmail.com")
		assertTrue testUser.validate()
	}
	
	void testGetOrCreatedAuthorizedUser() {
		mockDomain(DoubanUser, [])
		
		def doubanUser = DoubanUser.getOrCreatedAuthorizedUser([uid:'william', location:null])
		assertEquals 'william', doubanUser.uid
		assertEquals '', doubanUser.location
		assertEquals '', doubanUser.email
		assertEquals true, doubanUser.authorized
		assert DoubanUser.findByUid('william')
		
		doubanUser = DoubanUser.getOrCreatedAuthorizedUser([uid:'mary', location:'shanghai'])
		assertEquals 'mary', doubanUser.uid
		assertEquals 'shanghai', doubanUser.location
		assertEquals '', doubanUser.email
		assertEquals true, doubanUser.authorized
		assert DoubanUser.findByUid('mary')
	}
}
